//
// Copyright (c) 2009 All Right Reserved
//
// vl
//
// 2009-01-01
// Contains ...
using LargoCommon.Interfaces;
using System.Collections.Generic;
namespace LargoCommon.Music
{
/// Comparer of owners .
///
/// Enables comparing of Owners according to one given property.
///
/// Structure - the generic type parameter.
public sealed class ComparerGenStruct : IComparer
where T : IGeneralStruct {
#region Constructors
/// Initializes a new instance of the ComparerGenStruct class.
/// General musical property.
/// Sort direction.
public ComparerGenStruct(GenProperty property, GenSortDirection givenDirection) {
this.Property = property;
this.Direction = givenDirection;
}
#endregion
/// Gets property to be ordered.
/// Property description.
private GenProperty Property { get; }
/// Gets direction of ordered set.
/// Property description.
private GenSortDirection Direction { get; }
/// Compare property values of two given objects.
/// First object.
/// Second object.
/// Returns value.
int IComparer.Compare(T x, T y) {
if (x != null)
{
var fx = x.GetProperty(this.Property);
if (y != null)
{
var fy = y.GetProperty(this.Property);
if (this.Direction == GenSortDirection.Descending) {
if (fx > fy) {
return -1;
}
return fx < fy ? 1 : 0;
}
if (fx > fy) {
return 1;
}
if (fx < fy) {
return -1;
}
}
}
return 0;
}
}
}